#include <QPainter>
#include <QStyleOptionToolButton>

#include "toolbutton.h"

ToolButton::ToolButton(QWidget *parent) : QToolButton(parent),
    m_isHover(false),
    m_isPressed(false)
{
    //Update the toolbutton size.
    setFixedSize(22, 22);
}

void ToolButton::enterEvent(QEvent *)
{
    //Mark the hover state.
    m_isHover=true;
    //Update the button.
    update();
}

void ToolButton::leaveEvent(QEvent *)
{
    //Reset the hover state.
    m_isHover=false;
    //Update the button.
    update();
}

void ToolButton::mousePressEvent(QMouseEvent *)
{
    //Set pressed state.
    m_isPressed=true;
    update();
}

void ToolButton::mouseReleaseEvent(QMouseEvent *)
{
    m_isPressed=false;
    update();
}

void ToolButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QStyleOptionToolButton opt;
    initStyleOption(&opt);
    //Fill the background.
    if(m_isHover)
    {
        //Check pressed state.
        if(m_isPressed)
        {
            //Fill up with the color.
            painter.fillRect(rect(), QColor(207, 207, 207));
        }
        else
        {
            //Hover, but not pressed, normal color.
            painter.fillRect(rect(), QColor(223, 223, 223));
        }
    }
    //Draw the icon.
    QPixmap &&renderIcon = opt.icon.pixmap(size());
    painter.drawPixmap((opt.rect.width()-renderIcon.width())>>1,
                       (opt.rect.height()-renderIcon.height())>>1,
                       renderIcon);
}
